// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import {MathUtils} from "./math/MathUtils";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Rocker extends cc.Component {

  private ball: cc.Node;
  private rockerBg: cc.Node;

  circleRadius = 80;
  ballRadius = 20;

  dis = (this.circleRadius - this.ballRadius) * (this.circleRadius - this.ballRadius);

  onLoad() {
    this.ball = this.node.getChildByName('ball')
    this.rockerBg = this.node.getChildByName('rockerBg')
    this.deactive();
  }

  active(x, y) {
    // this.node.active = true;
    // this.ball.x = 0;
    // this.ball.y = 0;
    // this.node.x = x - this.node.parent.width / 2;
    // this.node.y = y - this.node.parent.height / 2;
  }

  deactive() {
    // this.node.active = false;
    this.ball.x = 0;
    this.ball.y = 0;
  }

  onTouchMove(deltaX: number, deltaY: number) {
    const radiusPow2 = deltaX * deltaX + deltaY * deltaY
    if (radiusPow2 > this.dis) {
      const radio = Math.sqrt(this.dis / radiusPow2)
      this.ball.x = deltaX * radio;
      this.ball.y = deltaY * radio;
    } else {
      this.ball.x = deltaX;
      this.ball.y = deltaY;
    }
    this.rockerBg.rotation = Math.atan2(this.ball.x, this.ball.y) * 180 / MathUtils.PI - 90;
  }

  onEnable() {

  }
}
